﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QLNS_Client
{
    public partial class UC_CanBo_NgoaiNgu : UserControl
    {
        string temp = "";
        string temp1 = "";
        string temp2 = "";
        string flag = "";
        ServiceReference1.Service_QLNSClient sv = new ServiceReference1.Service_QLNSClient();
        public UC_CanBo_NgoaiNgu()
        {
            InitializeComponent();
        }
        private void UC_CanBo_NgoaiNgu_Load(object sender, EventArgs e)
        {
            Load_CB_NN();

            //Hiển thị tùy chọn ngoại ngữ
            cmb_MaNN_NN.DataSource = sv.getNN();
            cmb_MaNN_NN.DisplayMember = "TenNN";
            cmb_MaNN_NN.ValueMember = "Ma_NN";

            //Hiển thị tùy chọn trình độ ngoại ngữ
            cmb_ThemMaTDNN_NN.DataSource = sv.getTDNN();
            cmb_ThemMaTDNN_NN.DisplayMember = "TenTDNN";
            cmb_ThemMaTDNN_NN.ValueMember = "Ma_TDNN";
            
            //Hiển thị tùy chọn xếp loại
            cmb_XapLoai_NN.DataSource = sv.getCB_TDNN();
            cmb_XapLoai_NN.DisplayMember = "XepLoai";
            cmb_XapLoai_NN.ValueMember = "Ma_TDNN";
            cmb_XapLoai_NN.ValueMember = "Ma_CB";
            cmb_XapLoai_NN.ValueMember = "Ma_NN";

            cmb_MaCB_NN.DataSource = sv.getCB_TDNN();
            cmb_MaCB_NN.DisplayMember = "Ma_CB";
            cmb_MaCB_NN.ValueMember = "Ma_CB";
        }
        public void Load_CB_NN()
        {
            dataGridView_NN.DataSource = sv.getCB_TDNN();
            btn_Luu.Enabled = false;
            btn_Huy.Enabled = false;
        }
        private void btn_ThemMaNN_NN_Click(object sender, EventArgs e)
        {
            frm_NN themnn = new frm_NN();
            themnn.ShowDialog();
            

        }

        private void btn_GoiBang_TDNN_CB_NN_Click(object sender, EventArgs e)
        {
            frm_TDNN themtdnn = new frm_TDNN();
            themtdnn.ShowDialog();
        }

        private void dataGridView_NN_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            cmb_MaCB_NN.Text = dataGridView_NN.CurrentRow.Cells[0].Value.ToString();
            cmb_MaNN_NN.Text = dataGridView_NN.CurrentRow.Cells[1].Value.ToString();
            cmb_ThemMaTDNN_NN.Text = dataGridView_NN.CurrentRow.Cells[2].Value.ToString();
            date_NgayCap_TDNN.Text = dataGridView_NN.CurrentRow.Cells[3].Value.ToString();
            txt_NoiCap_NN.Text = dataGridView_NN.CurrentRow.Cells[4].Value.ToString();
            cmb_XapLoai_NN.Text = dataGridView_NN.CurrentRow.Cells[5].Value.ToString();
            
            temp = dataGridView_NN.CurrentRow.Cells[0].Value.ToString();
            temp1 = dataGridView_NN.CurrentRow.Cells[1].Value.ToString();
            temp2 = dataGridView_NN.CurrentRow.Cells[2].Value.ToString();
            
            cmb_MaCB_NN.Enabled = false;
            cmb_MaNN_NN.Enabled = false;
            cmb_ThemMaTDNN_NN.Enabled = false;
            cmb_XapLoai_NN.Enabled = false;
            date_NgayCap_TDNN.Enabled = false;
            txt_NoiCap_NN.Enabled = false;
        }

        private void cmb_MaNN_NN_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btn_Them_Click(object sender, EventArgs e)
        {
            flag = "them";
            btn_Luu.Enabled = true;
            btn_Huy.Enabled = true;
            btn_Sua.Enabled = false;
            btb_Xoa.Enabled = false;
            cmb_MaCB_NN.Enabled = true;
            cmb_MaNN_NN.Enabled = true;
            cmb_ThemMaTDNN_NN.Enabled = true;
            cmb_XapLoai_NN.Enabled = true;
            date_NgayCap_TDNN.Enabled = true;
            txt_NoiCap_NN.Enabled = true;
            cmb_MaCB_NN.Text = "";
            cmb_MaNN_NN.Text = "";
            cmb_ThemMaTDNN_NN.Text = "";
            cmb_XapLoai_NN.Text = "";
            date_NgayCap_TDNN.Text = "";
            txt_NoiCap_NN.Text = "";
        }

        private void btn_Luu_Click(object sender, EventArgs e)
        {
            btn_Luu.Enabled = true;
            btb_Xoa.Enabled = true;
            btn_Sua.Enabled = true;

            ServiceReference1.CANBO_TRINHDO_NN cbtdnn = new ServiceReference1.CANBO_TRINHDO_NN();
            string a = cmb_MaCB_NN.Text;
            string b = cmb_MaNN_NN.SelectedValue.ToString();
            string c = cmb_ThemMaTDNN_NN.SelectedValue.ToString();
            string d = Convert.ToString(date_NgayCap_TDNN.Text);
            string f = txt_NoiCap_NN.Text;
            string k = cmb_XapLoai_NN.SelectedValue.ToString();
            cbtdnn.Ma_CB = a;
            cbtdnn.Ma_NN = b;
            cbtdnn.Ma_TDNN = c;
            cbtdnn.NgayCap = Convert.ToDateTime(d);
            cbtdnn.NoiCap = f;
            cbtdnn.XepLoai = k;
            if (a == "" | b == "" | c == "" | d == "" | f == "" | k == "")
            {
                MessageBox.Show("Xin nhập đầy đủ dữ liệu!");
            }
            else
            {
                int kq = 0;
                if (flag == "them")
                {
                    if (sv.checkCB_TDNN(cbtdnn))
                        kq = -1;
                    else
                        sv.addCB_TDNN(cbtdnn);
                }
                else
                {
                    if (flag == "sua")
                    {
                        sv.setCB_TDNN(cbtdnn);
                        kq = 1;
                    }
                }
                if (kq != -1)
                {
                    MessageBox.Show("Hoàn tất!");
                    Load_CB_NN();
                    btb_Xoa.Enabled = true;
                    btn_Sua.Enabled = true;
                }
                else
                {
                    MessageBox.Show("Lỗi!!!");
                }
            }
        }

        private void btn_Huy_Click(object sender, EventArgs e)
        {
            btn_Luu.Enabled = false;
            btn_Huy.Enabled = false;
            btn_Sua.Enabled = true;
            btb_Xoa.Enabled = true;
        }

        private void btb_Xoa_Click(object sender, EventArgs e)
        {
            ServiceReference1.CANBO_TRINHDO_NN cbtdnn = new ServiceReference1.CANBO_TRINHDO_NN();
            cbtdnn.Ma_CB = temp;
            cbtdnn.Ma_NN = temp1;
            cbtdnn.Ma_TDNN = temp2;
            DialogResult dr = MessageBox.Show("Bạn thật sự muốn xóa dữ liệu này?", "warnring", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                sv.deleteCB_TDNN(cbtdnn);
                Load_CB_NN();
                MessageBox.Show("Đã xóa!");
                cmb_MaCB_NN.Text = "";
                cmb_MaNN_NN.Text = "";
                cmb_ThemMaTDNN_NN.Text = "";
                date_NgayCap_TDNN.Text = "";
                txt_NoiCap_NN.Text = "";
                cmb_XapLoai_NN.Text = "";
            }
        }

        private void btn_Sua_Click(object sender, EventArgs e)
        {
            flag = "sua";
            cmb_MaCB_NN.Enabled = false;
            cmb_MaNN_NN.Enabled = false;
            cmb_ThemMaTDNN_NN.Enabled = false;
            cmb_XapLoai_NN.Enabled = true;
            date_NgayCap_TDNN.Enabled = true;
            txt_NoiCap_NN.Enabled = true;
            btn_Luu.Enabled = true;
            
        }

        


    
    }
}
